package com.bigstar.lolen_e;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Masteries_Utility extends Activity implements OnClickListener{
	public static final String[] title = {
		"Summoner's Insight", "Good Hands", "Expanded Mind", "Improved Recall",
		"Swiftness", "Meditation", "Scout",
		"Greed", "Transmutation", "Runic Affinity",
		"Wealth", "Awareness", "Sage",
		"Strength of Spirit", "Intelligence",
		"Mastermind",
	};
	public static final String[] text = {
		"Improves the following summoner spells:\r\nTeleport: Reduces cast time by 1 second.\r\nPromote: Increases promoted minion's bonus defensive stats by 15%.\r\nFlash: Reduces cooldown by 15 seconds.\r\nClarity: Increases mana restored by 20%.\r\nClairvoyance: Increases duration by 2 seconds.",
		"Reduces time spent dead by num%.",
		"+num Mana per level\r\n(+1n1 Mana at champion level 18)or +2n2 Energy",
		"Reduces the cast time of Recall by num second and Enhanced Recall by 0.5 seconds.",
		"+num% Movement Speed",
		"+num Mana Regen per 5 seconds",
		"Increases vision range of wards by num%",
		"Gain an additional num gold every 10 seconds",
		"+num% Spell Vamp",
		"Increases the duration of shrine, relic, quest, and neutral monster buffs by num%",
		"Increases starting gold by num.",
		"Increases experience gained by num%",
		"Gain num bonus experience on champion kills and assists(50% reduced effect on the Crystal Scar).",
		"Increases Health Regen per 5 seconds by 0.4% of maximum Mana.",
		"+num% Cooldown Reduction",
		"Reduces the cooldown of your Summoner Spells by num%"
	};
	
	public static final int max_point[] = { 1,3,3,1,4,3,1,4,3,1,2,4,1,3,3,1 };
	public static final String[][] level = {
		{"0"},
		{"4","8","12"},
		{"4","8","12"},
		{"1"},
		{"0.5","1","1.5","2"},
		{"1","2","3"},
		{"5"},
		{"0.5","1","1.5","2"},
		{"1","2","3"},
		{"20"},
		{"20","40"},
		{"1.25","2.5","3.75","5"},
		{"40"},
		{"0.4","0.7","1"},
		{"2","4","6"},
		{"15"}
	};
	public int spellid = 0;
	public static Activity context;
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState); 
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
		setContentView(R.layout.masteries_utility); 
		context = this;
 		if(getIntent().getExtras() != null){
 			ImageView iv = null;
 			for(int i = 0; i < 16; i++){
 				iv = (ImageView)findViewById(R.id.u_00+i);
 				iv.setOnClickListener(this);
 			}
 			TextView tv = (TextView)findViewById(R.id.b_utility);
			tv.setText("UTILITY:  "+Builder.getMasteriesPoint(2));
			imageChange();
 		}else{
 			ImageView iv = null;
 			for(int i = 0; i < 16; i++){
 				iv = (ImageView)findViewById(R.id.u_00+i);
 				iv.setOnClickListener(this);
 			}
 		}
	}
	
    @Override
    protected void onResume() {
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
    	super.onResume();
 		if(getIntent().getExtras() != null){
 			ImageView iv = null;
 			for(int i = 0; i < 16; i++){
 				iv = (ImageView)findViewById(R.id.u_00+i);
 				iv.setOnClickListener(this);
 			}
 			TextView tv = (TextView)findViewById(R.id.b_utility);
			tv.setText("UTILITY:  "+Builder.getMasteriesPoint(2));
			imageChange();
 		}
    }
	
	public static void imageChange(){
		ImageView iv = null;
		Bitmap bitmap = null;
		TextView tv = null;
		tv = (TextView)context.findViewById(R.id.b_utility);
		tv.setText("UTILITY:  "+Builder.getMasteriesPoint(2));
		for(int i = 0; i < 16; i++){
			tv = (TextView)context.findViewById(R.id.u_text_00+i);
			tv.setText(" "+Builder.masteries[2][i]+"/"+max_point[i]);
			switch(i){
			case 4: 
			case 6:
				if(Builder.getMasteriesPoint(2) < 4){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 5:
				if(Builder.masteries[2][2] < 3){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					
					iv = (ImageView)context.findViewById(R.id.m_p_00);
					iv.clearColorFilter();
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
					iv.setImageBitmap(bitmap);
					continue;
				}
				if(Builder.getMasteriesPoint(2) < 4){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				iv = (ImageView)context.findViewById(R.id.m_p_00);
				iv.setColorFilter(Color.GREEN, PorterDuff.Mode.DARKEN);
				bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
				iv.setImageBitmap(bitmap);
				break;
			case 7:
			case 8:
			case 9:
				if(Builder.getMasteriesPoint(2) < 8){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 10:
				if(Builder.masteries[2][7] < 4){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					
					iv = (ImageView)context.findViewById(R.id.m_p_01);
					iv.clearColorFilter();
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
					iv.setImageBitmap(bitmap);
					continue;
				}
				if(Builder.getMasteriesPoint(2) < 12){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				iv = (ImageView)context.findViewById(R.id.m_p_01);
				iv.setColorFilter(Color.GREEN, PorterDuff.Mode.DARKEN);
				bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
				iv.setImageBitmap(bitmap);
				break;
			case 11:
			case 12:
				if(Builder.getMasteriesPoint(2) < 12){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 13:
			case 14:
				if(Builder.getMasteriesPoint(2) < 16){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 15:
				if(Builder.getMasteriesPoint(2) < 20){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			}
			iv = (ImageView)context.findViewById(R.id.u_00+i);
			iv.clearColorFilter();
			bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
			iv.setImageBitmap(bitmap);
		}
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.b_champ_m_minus:
			if(Builder.masteries[2][spellid] > 0){
				Builder.masteries[2][spellid]--;
				set();
			}
			break;
		case R.id.b_champ_m_plus:
			if(Builder.getMasteriesCount() >= 30){
				Toast.makeText(this, "Have 30 points, all-consuming", Toast.LENGTH_LONG).show();
				return;
			}
			switch(spellid){
			case 4: 
			case 6:
				if(Builder.getMasteriesPoint(2) < 4){
					return;
				}
				break;
			case 5:
				if(Builder.masteries[2][2] < 3){
					return;
				}
				if(Builder.getMasteriesPoint(2) < 4){
					return;
				}
				break;
			case 7: 
			case 8:
			case 9:
				if(Builder.getMasteriesPoint(2) < 8){
					return;
				}
				break;
			case 10:
				if(Builder.masteries[2][7] < 4){
					return;
				}
				if(Builder.getMasteriesPoint(2) < 12){
					return;
				}
				break;
			case 11:
			case 12:
				if(Builder.getMasteriesPoint(2) < 12){
					return;
				}
				break;
			case 13:
			case 14:
				if(Builder.getMasteriesPoint(2) < 16){
					return;
				}
				break;
			case 15:
				if(Builder.getMasteriesPoint(2) < 20){
					return;
				}
				break;
			}
			if(Builder.masteries[2][spellid] < max_point[spellid]){
				Builder.masteries[2][spellid]++;
				set();
			}
			break;
		default:
			dialog(v.getId() - R.id.u_00);
			break;
		}
	}
	
	public void set(){
		TextView tv = (TextView)findViewById(R.id.b_utility);
		tv.setText("UTILITY:  "+Builder.getMasteriesPoint(2));
		tv = (TextView)findViewById(R.id.u_text_00+spellid);
		tv.setText(" "+Builder.masteries[2][spellid]+"/"+max_point[spellid]);
		tv = (TextView) layout.findViewById(R.id.b_champ_m_text);
		tv.setText("rank: "+Builder.masteries[2][spellid]+"/"+max_point[spellid]+"\r\n\r\n"+text[spellid]);
		String limit = "";
		switch(spellid){
		case 4:
		case 6: limit = "\r\nUtility 4Points needed\r\n"; break;
		case 5: limit = "\r\nUtility 4Points needed\r\nExpanded Mind 3Points needed\r\n"; break;
		case 7:
		case 8:
		case 9: limit = "\r\nUtility 8Points needed\r\n"; break;
		case 10: limit = "\r\nUtility 12Points needed\r\nGreed 3Points needed\r\n"; break;
		case 11:
		case 12: limit = "\r\nUtility 12Points needed\r\n"; break;
		case 13: 
		case 14: limit = "\r\nUtility 16Points needed\r\n"; break;
		case 15: limit = "\r\nUtility 20Points needed\r\n"; break;
		}
		if(Builder.masteries[2][spellid] >= max_point[spellid]){
			String t = text[spellid];
			t = t.replaceAll("num", level[spellid][Builder.masteries[2][spellid]-1]);
			if(spellid == 2){
				float num2 = Float.parseFloat(level[spellid][Builder.masteries[2][spellid]-1]);
				t = t.replaceAll("1n1", ""+(num2*18));
				String n2[] = {"4","7","10"};
				t = t.replaceAll("2n2", n2[Builder.masteries[2][spellid]-1]);
			}
			tv.setText("Rank: "+Builder.masteries[2][spellid]+"/"+max_point[spellid]+"\r\n"+limit+"\r\n"+t);	
		}else if(Builder.masteries[2][spellid] == 0){
			String t = text[spellid];
			t = t.replaceAll("num", level[spellid][Builder.masteries[2][spellid]]);
			if(spellid == 2){
				float num2 = Float.parseFloat(level[spellid][Builder.masteries[2][spellid]]);
				t = t.replaceAll("1n1", ""+(num2*18));
				String n2[] = {"4","7","10"};
				t = t.replaceAll("2n2", n2[Builder.masteries[2][spellid]]);
			}
			tv.setText("Rank: "+Builder.masteries[2][spellid]+"/"+max_point[spellid]+"\r\n"+limit+"\r\n"+t);	
		}else{
			String t = text[spellid];
			t = t.replaceAll("num", level[spellid][Builder.masteries[2][spellid]-1]);
			if(spellid == 2){
				float num2 = Float.parseFloat(level[spellid][Builder.masteries[2][spellid]-1]);
				t = t.replaceAll("1n1", ""+(num2*18));
				String n2[] = {"4","7","10"};
				t = t.replaceAll("2n2", n2[Builder.masteries[2][spellid]-1]);
			}
			String next = text[spellid];
			next = next.replaceAll("num", level[spellid][Builder.masteries[2][spellid]]);
			if(spellid == 2){
				float num2 = Float.parseFloat(level[spellid][Builder.masteries[2][spellid]]);
				next = next.replaceAll("1n1", ""+(num2*18));
				String n2[] = {"4","7","10"};
				next = next.replaceAll("2n2", n2[Builder.masteries[2][spellid]]);
			}
			tv.setText("Rank: "+Builder.masteries[2][spellid]+"/"+max_point[spellid]+"\r\n"+limit+"\r\n"+t
			          +"\r\n\r\nNextRank:\r\n"+next);	
		}
	}
	
	View layout;
	
	private void dialog(int id) {  
 		if(getIntent().getExtras() != null){
 			spellid = id;
 			LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
 			layout = inflater.inflate(R.layout.build_masteries,(ViewGroup) findViewById(R.id.b_champ_m));
 			set();
 			Button bt = (Button)layout.findViewById(R.id.b_champ_m_minus);
 			bt.setOnClickListener(this);
 			bt = (Button)layout.findViewById(R.id.b_champ_m_plus);
 			bt.setOnClickListener(this);
 			AlertDialog.Builder alt_bld = new AlertDialog.Builder(getParent()); 
 			alt_bld
 			.setView(layout)
 			.setCancelable(false).setNegativeButton("Close",  
 					new DialogInterface.OnClickListener() {
 				public void onClick(DialogInterface dialog, int id) {
 					imageChange();
 					dialog.cancel();
 				}});  
 			AlertDialog alert = alt_bld.create();  
 				alert.setTitle(title[id]);
 				alert.setIcon(R.drawable.u_00+id);
 				alert.show(); 
 		}else{
			String num = "";
			String t = text[id];
			for(int i = 0; i < level[id].length; i++){
				num = num+level[id][i];
				if(level[id].length == 1){
				}else if(i+1 < level[id].length){
					num = num+"/";
				}
			}
			t = t.replaceAll("num", num);
			if(id == 2){
				t = t.replaceAll("1n1", "72/144/216");
				t = t.replaceAll("2n2", "4/7/10");
			}
			TextView tv = new TextView(this);
			tv.setText(t);
			tv.setTextSize(14);
 			AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); 
 			alt_bld.setView(tv).setNegativeButton("Close",  
 					new DialogInterface.OnClickListener() {
 				public void onClick(DialogInterface dialog, int id) {
 					dialog.cancel();
 				}});  
 			AlertDialog alert = alt_bld.create();  
 				alert.setTitle(title[id]);
 				alert.setIcon(R.drawable.u_00+id);
 				alert.show(); 	
 		}
	}
}